<script setup>
import { RouterLink, RouterView, routerKey } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref, reactive, onMounted } from 'vue'
import { default as http, getImageUrl } from '@/utils/request.js';
import { useRouter} from 'vue-router'; //路由器
const router = useRouter(); //获取到 router 对象，用于编程式跳转导航
const active = ref(1);
const onClickLeft = () => router.push("/personal");
const toMessage = () => router.push("/message");

// Alt + Shift +F 整理代码格式

// 待发货列表
const backOrderList = reactive([

])
// 获取待发货列表  
const getBackOrderList = () => {
  // console.log("------------" + user.value);
  http.get("/user/info").then((response) => {
    console.log("获取个人信息= ", response);
    console.log("id:", response.data.data.userName);
    http.get("/user/order/backorder/" + response.data.data.userName).then((res) => {
      backOrderList.splice(0); //todo 需要将原有的元素清空  
      // afterSendList = afterSendList.filter(res.data.data.list.orderNumber); 
      res.data.data.list.forEach(order => {
        backOrderList.push(order);
      });
      console.log('获取待发货列表: ', res.data.data.list);
      console.log('backOrderList:', backOrderList);
      // resolve(); // 当获取待收货列表成功后，返回一个解析成功的Promise  
    }).catch(err => {
      reject(err); // 当请求失败时，返回一个请求失败的Promise  
    });

  }).catch(() => {
  });
};

// 打开订单
// '/orderinfo/:orderNumber(\\d+)',
const toOrder = function (orderNumber) {
    console.log("订单号:", orderNumber);
    // router.push({path: "/orderinfo/" + orderNumber});
    router.push({path: '/orderinf/' + orderNumber});
}
// 取消订单，退款方法TODO
const handleCancel = (orderNumber) => {
  console.log("------------退款方法");
  // 二次确认
  ElMessageBox.confirm(`确定要取消此订单吗？`, '提示', {
    type: 'warning'
  }).then(() => {//用户点击确定按钮
      http.get("/return/"+orderNumber)
        .then((res) => {
          console.log('返回值：', res);
          if (res.data.status === 200) {	//修改成功
            ElMessage.success(`取消成功`);
            getBackOrderList;
          }
        }).catch((err) => {
          console.log('取消失败：', err);
          ElMessage.error(`取消失败`);
        });
    })
    .catch(() => {//点击取消按钮

    });
};


onMounted(() => {
  // getparseJWT();
  getBackOrderList();
  // handleDelete(); 
  // 获取个人信息  

});
</script>
<template>
  <!-- <header style="padding-top:2rem;">
    <div style="width: 350px ; ">
      <van-cell-group style="margin-left: 0; "> -->
        <van-row>
          <van-nav-bar title="待发货" left-text="返回" :fixed="true" left-arrow @click-left="onClickLeft" />
        </van-row>
      <!-- </van-cell-group> -->
      <van-cell-group>
        <van-tabs v-model:active="active" style="margin-top: 25px;">
          <van-tab title="待付款" to="/beforepay"></van-tab>
          <van-tab title="待发货"></van-tab>
          <van-tab title="待收货" to="/aftersend"></van-tab>
          <van-tab title="售 后" to="/returnorder"></van-tab>
        </van-tabs>
      </van-cell-group>
    <!-- </div>
  </header> -->
  <!-- <br> -->
  <div style="color: #c8d175; margin-top: 10px;">
    <van-cell-group inset>
        <van-card v-for="bo in backOrderList"  :key="bo.orderNumber" :num="bo.amount" :price="bo.orderPrice" :desc="bo.details"
          :title="bo.sellName" :thumb="getImageUrl(bo.picture)" style="margin-top: 10px; background-color:  #FFFFE0;"  @click="toOrder(bo.orderNumber)">
          <template #tags>
            <van-tag plain type="primary" style="flex-grow: 1; margin-right: 10px;">{{bo.goodsName}}</van-tag>
          </template>
          <template #footer style="margin-left: 0%;">
            <van-space size="1rem">
            <van-button size="mini" @click.stop="toMessage">催发货货</van-button>
            <van-button size="mini" @click.stop="handleCancel(bo.orderNumber)">取消订单</van-button>
          </van-space>
          </template>
        </van-card>
    </van-cell-group>
  </div>

  <RouterView />
</template>

<style scoped>
header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 9999;
  line-height: 1.5;
  max-height: 100vh;
}


header {
  line-height: 1.5;
  max-height: 100vh;
}


nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }


  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
    padding: 1rem 0;
    margin-top: 1rem;
  }
}

.van-field {
  /* 调整每一行边距 */
  padding: 16px 15px;
}

.van-nav-bar {
  color: #d4e09f;
  width: 100%;
}

.full-width-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 10px;
}
.nav_bar{
    position:relative;
    top:-32px;
    /* left:-32px;
    right: -32px; */
    margin:2px -32px
}
</style>
